package com.wisdytech.linkdcs.equipLifeCycle.check.action;


import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.handler.ResourceMatchingService;
import com.wisdytech.common.utils.DateUtil;
import com.wisdytech.common.utils.ExcelUtil;
import com.wisdytech.linkdcs.equipLifeCycle.check.dto.CheckMonthSearchDto;
import com.wisdytech.linkdcs.equipLifeCycle.check.dto.CheckRecordSearchDto;
import com.wisdytech.linkdcs.equipLifeCycle.check.dto.ErrorEqDto;
import com.wisdytech.linkdcs.equipLifeCycle.check.service.ICheckDetailRecordService;
import com.wisdytech.linkdcs.equipLifeCycle.check.service.ICheckRecordService;
import com.wisdytech.linkdcs.equipLifeCycle.check.vo.CheckMonthVo;
import com.wisdytech.linkdcs.equipLifeCycle.check.vo.CheckRecordDetailVo;
import com.wisdytech.linkdcs.equipLifeCycle.check.vo.CheckRecordVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dailibo
 * @since 2018-06-05
 */
@Controller
@RequestMapping("/equipLifeCycle/check/checkRecord")
public class CheckRecordController {
	
	private Logger logger =LoggerFactory.getLogger(getClass());
	
	@Autowired
	private ICheckRecordService iCheckRecordService;
	

    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
	@Autowired
	private ICheckDetailRecordService iCheckDetailRecordService;
	@RequestMapping("/index")
	public String checkRecordIndex(Model model,HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
		return "lifeCycle/equipLifeCycle/check/recordIndex";
	}
	
	@RequestMapping("/month/index")
	public String checkMonthRecordIndex(Model model,HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
		return "lifeCycle/equipLifeCycle/check/monthRecord";
	}
	
	@RequestMapping("/listRecords/get.mvc")
	@ResponseBody
	public StandardTableResult<CheckRecordVo> listCheckRecords(Filter filter,CheckRecordSearchDto sDto){
		PageInfo<CheckRecordVo>  pageInfo =
				iCheckRecordService.listCheckRecord(filter, sDto);
		StandardTableResult<CheckRecordVo> tables=
				new StandardTableResult<CheckRecordVo>(pageInfo.getTotal(),pageInfo.getList());
		
		return tables;
	}
	
	@RequestMapping("/getRecordDetail/get.mvc")
	@ResponseBody
	public StandardTableResult<CheckRecordDetailVo> getCheckRecordDetail(String recordId){

		List<CheckRecordDetailVo> details=
				iCheckDetailRecordService.getCheckDetail(recordId);
		
		StandardTableResult<CheckRecordDetailVo> tables=
				new StandardTableResult<CheckRecordDetailVo>(Long.valueOf(details.size()),details);
		
		return tables;
		
	}
	
	@RequestMapping("/getMonthRecordDetail/get.mvc")
	@ResponseBody
	public StandardTableResult<CheckRecordDetailVo> getMonthCheckRecordDetail(
			String eqCode,String checkItemCode,String year,String month,String day){

		List<CheckRecordDetailVo> details=
				iCheckRecordService.getMonthCheckDetail(eqCode, checkItemCode, year, month, day);
		
		
		StandardTableResult<CheckRecordDetailVo> tables=
				new StandardTableResult<CheckRecordDetailVo>(Long.valueOf(details.size()),details);
		
		return tables;
		
	}
	
	
	/****月度点检记录****/
	@RequestMapping("/getMonthRecord/get.mvc")
	@ResponseBody
	public StandardTableResult<CheckMonthVo> getMonthRecord(Filter filter,CheckMonthSearchDto dto){
		
		Date date =dto.getDate();
		Calendar cal=Calendar.getInstance();
		cal.setTime(date);
		int year = cal.get(Calendar.YEAR);//获取年份
        int month=cal.get(Calendar.MONTH)+1;//获取月份
		PageInfo<CheckMonthVo>  records =
				iCheckRecordService.getMonthCheckRecord(
						filter, dto, String.valueOf(year),
						String.valueOf(month));
		
		StandardTableResult<CheckMonthVo> tables=
				new StandardTableResult<CheckMonthVo>(records.getTotal(),records.getList());
		
		return tables;
		
	}
	
	/****月度点检记录****/
	@RequestMapping("/getTop10/get.mvc")
	@ResponseBody
	public StandardResult listTop10ErrorEq(String date){
		
		Date dateN =DateUtil.getMonthDate(date);
		Calendar cal=Calendar.getInstance();
		cal.setTime(dateN);
		int year = cal.get(Calendar.YEAR);//获取年份
        int month=cal.get(Calendar.MONTH)+1;//获取月份
        List<ErrorEqDto>  listDatas =iCheckRecordService.listTop10ErrorEq(String.valueOf(year),
				String.valueOf(month));
		return StandardResult.getSuccessResult(listDatas);
		
	}
	
	/****月度点检记录
	 * @throws Exception ****/
	@RequestMapping("/exportRecord/get.mvc")
	@ResponseBody
	public void exportRecord(CheckMonthSearchDto dto,HttpServletRequest request, HttpServletResponse response) throws Exception{
		Filter filter =new Filter();
		filter.setNum(0);
		filter.setSize(Integer.MAX_VALUE);
		
		Date date =dto.getDate();
		Calendar cal=Calendar.getInstance();
		cal.setTime(date);
		int year = cal.get(Calendar.YEAR);//获取年份
        int month=cal.get(Calendar.MONTH)+1;//获取月份
		PageInfo<CheckMonthVo>  records =
				iCheckRecordService.getMonthCheckRecord(
						filter, dto, String.valueOf(year),
						String.valueOf(month));
		List<CheckMonthVo> list = records.getList();
		for(CheckMonthVo vo : list){
			this.changeValue(vo);

		
		
		}
		SimpleDateFormat dateFormat;
        dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String[] columnNames = null;
        String[] fileNames = null;
        String fileName = "月度点检表";
		String finalFileName = null;  
        final String userAgent = request.getHeader("USER-AGENT"); 
        if(StringUtils.contains(userAgent, "MSIE")){
            //IE浏览器  
            finalFileName = URLEncoder.encode(fileName,"UTF8");  
        }else {
            finalFileName = fileName;
        }
        
        columnNames = new String[] {
        		"设备编号", "设备名称", "点检项目编号", 
        		"1","2","3","4","5","6","7","8","9","10",
        		"11","12","13","14","15","16","17","18","19","20",
        		"21","22","23","24","25","26","27","28","29","30",
        		"31"};
        fileNames = new String[] { 
        		"eqCode", "eqName", "checkModelCode",
        		"value1","value2","value3","value4","value5","value6","value7","value8","value9","value10",
        		"value11","value12","value13","value14","value15","value16","value17","value18","value19","value20",
        		"value21","value22","value23","value24","value25","value26","value27","value28","value29","value30",
        		"value31"};
        
        
        ExcelUtil.exportExcel(response, records.getList(), CheckMonthVo.class, columnNames, fileNames,
                finalFileName + dateFormat.format(new Date()));
		
		
		
	}
	private void changeValue(CheckMonthVo vo) {
		if (StringUtils.isNotEmpty(vo.getValue1())){
			if(vo.getValue1().equals("-1")){
				vo.setValue1("×");
			}
			if(vo.getValue1().equals("1")){
				vo.setValue1("√");
			}
		}
		if (StringUtils.isNotEmpty(vo.getValue2())){
			if(vo.getValue2().equals("-1")){
				vo.setValue2("×");
			}
			if(vo.getValue2().equals("1")){
				vo.setValue2("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue3())){
			if(vo.getValue3().equals("-1")){
				vo.setValue3("×");
			}
			if(vo.getValue3().equals("1")){
				vo.setValue3("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue4())){
			if(vo.getValue4().equals("-1")){
				vo.setValue4("×");
			}
			if(vo.getValue4().equals("1")){
				vo.setValue4("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue5())){
			if(vo.getValue5().equals("-1")){
				vo.setValue5("×");
			}
			if(vo.getValue5().equals("1")){
				vo.setValue5("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue6())){
			if(vo.getValue6().equals("-1")){
				vo.setValue6("×");
			}
			if(vo.getValue6().equals("1")){
				vo.setValue6("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue7())){
			if(vo.getValue7().equals("-1")){
				vo.setValue7("×");
			}
			if(vo.getValue7().equals("1")){
				vo.setValue7("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue8())){
			if(vo.getValue8().equals("-1")){
				vo.setValue8("×");
			}
			if(vo.getValue8().equals("1")){
				vo.setValue8("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue9())){
			if(vo.getValue9().equals("-1")){
				vo.setValue9("×");
			}
			if(vo.getValue9().equals("1")){
				vo.setValue9("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue10())){
			if(vo.getValue10().equals("-1")){
				vo.setValue10("×");
			}
			if(vo.getValue10().equals("1")){
				vo.setValue10("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue11())){
			if(vo.getValue11().equals("-1")){
				vo.setValue11("×");
			}
			if(vo.getValue11().equals("1")){
				vo.setValue11("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue12())){
			if(vo.getValue12().equals("-1")){
				vo.setValue12("×");
			}
			if(vo.getValue12().equals("1")){
				vo.setValue12("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue13())){
			if(vo.getValue13().equals("-1")){
				vo.setValue13("×");
			}
			if(vo.getValue13().equals("1")){
				vo.setValue13("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue14())){
			if(vo.getValue14().equals("-1")){
				vo.setValue14("×");
			}
			if(vo.getValue14().equals("1")){
				vo.setValue14("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue15())){
			if(vo.getValue15().equals("-1")){
				vo.setValue15("×");
			}
			if(vo.getValue15().equals("1")){
				vo.setValue15("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue16())){
			if(vo.getValue16().equals("-1")){
				vo.setValue16("×");
			}
			if(vo.getValue16().equals("1")){
				vo.setValue16("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue17())){
			if(vo.getValue17().equals("-1")){
				vo.setValue17("×");
			}
			if(vo.getValue17().equals("1")){
				vo.setValue17("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue18())){
			if(vo.getValue18().equals("-1")){
				vo.setValue18("×");
			}
			if(vo.getValue18().equals("1")){
				vo.setValue18("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue19())){
			if(vo.getValue19().equals("-1")){
				vo.setValue19("×");
			}
			if(vo.getValue19().equals("1")){
				vo.setValue19("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue20())){
			if(vo.getValue20().equals("-1")){
				vo.setValue20("×");
			}
			if(vo.getValue20().equals("1")){
				vo.setValue20("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue21())){
			if(vo.getValue21().equals("-1")){
				vo.setValue21("×");
			}
			if(vo.getValue21().equals("1")){
				vo.setValue21("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue22())){
			if(vo.getValue22().equals("-1")){
				vo.setValue22("×");
			}
			if(vo.getValue22().equals("1")){
				vo.setValue22("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue23())){
			if(vo.getValue23().equals("-1")){
				vo.setValue23("×");
			}
			if(vo.getValue23().equals("1")){
				vo.setValue23("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue24())){
			if(vo.getValue24().equals("-1")){
				vo.setValue24("×");
			}
			if(vo.getValue24().equals("1")){
				vo.setValue24("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue25())){
			if(vo.getValue25().equals("-1")){
				vo.setValue25("×");
			}
			if(vo.getValue25().equals("1")){
				vo.setValue25("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue26())){
			if(vo.getValue26().equals("-1")){
				vo.setValue26("×");
			}
			if(vo.getValue26().equals("1")){
				vo.setValue26("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue27())){
			if(vo.getValue27().equals("-1")){
				vo.setValue27("×");
			}
			if(vo.getValue27().equals("1")){
				vo.setValue27("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue28())){
			if(vo.getValue28().equals("-1")){
				vo.setValue28("×");
			}
			if(vo.getValue28().equals("1")){
				vo.setValue28("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue29())){
			if(vo.getValue29().equals("-1")){
				vo.setValue29("×");
			}
			if(vo.getValue29().equals("1")){
				vo.setValue29("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue30())){
			if(vo.getValue30().equals("-1")){
				vo.setValue30("×");
			}
			if(vo.getValue30().equals("1")){
				vo.setValue30("√");
			}
		}

		if (StringUtils.isNotEmpty(vo.getValue31())){
			if(vo.getValue31().equals("-1")){
				vo.setValue31("×");
			}
			if(vo.getValue31().equals("1")){
				vo.setValue31("√");
			}
		}


	}
	

}

